/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.ProfileManager.UIBuilders;

import android.isisufes.R;
import android.isisufes.ProfileManager.profile.Profile;
import android.isisufes.ProfileManager.profile.Social;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

/*
 * this class does all the programatic UI work
 * it is also responsable for exporting a object containing the data
 * acquired from the UI  
 */
public class SocialUI extends ProfileUI {
	View myView;

	boolean[] humor;
	boolean[] fashion;
	boolean[] living;
	
	boolean changed = false;

	Spinner children, ethinicity, religion, polView, sexOri, smoking, drinking,
			pets;


	
	public SocialUI() {
		this.layoutID = R.layout.social;
	}

	public void SetUpSpinners(View v) {
		Spinner s = children = (Spinner) v.findViewById(R.id.children_chooser);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(v
				.getContext(), R.array.children,
				android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		ethinicity = s = (Spinner) v.findViewById(R.id.ethinic_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.ethnicity, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		religion = s = (Spinner) v.findViewById(R.id.religion_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.religion, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		polView = s = (Spinner) v.findViewById(R.id.political_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.political, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		sexOri = s = (Spinner) v.findViewById(R.id.sexual_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.sex_pref, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		smoking = s = (Spinner) v.findViewById(R.id.smoking_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.smoking, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		drinking = s = (Spinner) v.findViewById(R.id.drinking_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.drinking, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		pets = s = (Spinner) v.findViewById(R.id.pets_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(), R.array.pets,
				android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

	}

	private void SetUpButtons() {
		Button humor = (Button) myView.findViewById(R.id.humor_button);
		humor.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final Dialog humorDialog = new Dialog(v.getContext());
				humorDialog.setContentView(R.layout.humor_dialog);
				
				if(SocialUI.this.humor!=null){
					((CheckBox)humorDialog.findViewById(R.id.humor_cb0)).setChecked(SocialUI.this.humor[0]);
					((CheckBox)humorDialog.findViewById(R.id.humor_cb1)).setChecked(SocialUI.this.humor[1]);
					((CheckBox)humorDialog.findViewById(R.id.humor_cb2)).setChecked(SocialUI.this.humor[2]);
					((CheckBox)humorDialog.findViewById(R.id.humor_cb3)).setChecked(SocialUI.this.humor[3]);
					((CheckBox)humorDialog.findViewById(R.id.humor_cb4)).setChecked(SocialUI.this.humor[4]);
					((CheckBox)humorDialog.findViewById(R.id.humor_cb5)).setChecked(SocialUI.this.humor[5]);
				}
				
				
				
				Button todo = (Button) humorDialog
						.findViewById(R.id.humor_quit);
				humorDialog.setTitle("Humor");
				todo.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						if(SocialUI.this.humor==null)SocialUI.this.humor = new boolean[6];
						SocialUI.this.humor[0]=((CheckBox)humorDialog.findViewById(R.id.humor_cb0)).isChecked();
						SocialUI.this.humor[1]=((CheckBox)humorDialog.findViewById(R.id.humor_cb1)).isChecked();
						SocialUI.this.humor[2]=((CheckBox)humorDialog.findViewById(R.id.humor_cb2)).isChecked();
						SocialUI.this.humor[3]=((CheckBox)humorDialog.findViewById(R.id.humor_cb3)).isChecked();
						SocialUI.this.humor[4]=((CheckBox)humorDialog.findViewById(R.id.humor_cb4)).isChecked();
						SocialUI.this.humor[5]=((CheckBox)humorDialog.findViewById(R.id.humor_cb5)).isChecked();
						humorDialog.dismiss();
					}
				});
				humorDialog.show();
			}
		});
		
		
		Button fashion = (Button) myView.findViewById(R.id.fashion_button);
		fashion.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final Dialog humorDialog = new Dialog(v.getContext());
				humorDialog.setContentView(R.layout.fashion_dialog);
				Button todo = (Button) humorDialog
						.findViewById(R.id.fashion_quit);
				humorDialog.setTitle("Fashion");
				
				if(SocialUI.this.fashion!=null){
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb0)).setChecked(SocialUI.this.fashion[0]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb1)).setChecked(SocialUI.this.fashion[1]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb2)).setChecked(SocialUI.this.fashion[2]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb3)).setChecked(SocialUI.this.fashion[3]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb4)).setChecked(SocialUI.this.fashion[4]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb5)).setChecked(SocialUI.this.fashion[5]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb6)).setChecked(SocialUI.this.fashion[6]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb7)).setChecked(SocialUI.this.fashion[7]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb8)).setChecked(SocialUI.this.fashion[8]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb9)).setChecked(SocialUI.this.fashion[9]);
					((CheckBox)humorDialog.findViewById(R.id.fashion_cb10)).setChecked(SocialUI.this.fashion[10]);
				}
				
				todo.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						if(SocialUI.this.fashion==null)SocialUI.this.fashion = new boolean[11];
						SocialUI.this.fashion[0]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb0)).isChecked();
						SocialUI.this.fashion[1]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb1)).isChecked();
						SocialUI.this.fashion[2]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb2)).isChecked();
						SocialUI.this.fashion[3]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb3)).isChecked();
						SocialUI.this.fashion[4]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb4)).isChecked();
						SocialUI.this.fashion[5]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb5)).isChecked();
						SocialUI.this.fashion[6]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb6)).isChecked();
						SocialUI.this.fashion[7]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb7)).isChecked();
						SocialUI.this.fashion[8]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb8)).isChecked();
						SocialUI.this.fashion[9]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb9)).isChecked();
						SocialUI.this.fashion[10]=((CheckBox)humorDialog.findViewById(R.id.fashion_cb10)).isChecked();
						humorDialog.dismiss();
					}
				});
				humorDialog.show();
			}
		});
		
		Button living = (Button) myView.findViewById(R.id.living_button);
		living.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final Dialog humorDialog = new Dialog(v.getContext());
				humorDialog.setContentView(R.layout.living_dialog);
				Button todo = (Button) humorDialog
						.findViewById(R.id.living_quit);
				humorDialog.setTitle("Living");
				
				
				if(SocialUI.this.living!=null){
					((CheckBox)humorDialog.findViewById(R.id.living_cb0)).setChecked(SocialUI.this.living[0]);
					((CheckBox)humorDialog.findViewById(R.id.living_cb1)).setChecked(SocialUI.this.living[1]);
					((CheckBox)humorDialog.findViewById(R.id.living_cb2)).setChecked(SocialUI.this.living[2]);
					((CheckBox)humorDialog.findViewById(R.id.living_cb3)).setChecked(SocialUI.this.living[3]);
					((CheckBox)humorDialog.findViewById(R.id.living_cb4)).setChecked(SocialUI.this.living[4]);
					((CheckBox)humorDialog.findViewById(R.id.living_cb5)).setChecked(SocialUI.this.living[5]);
					((CheckBox)humorDialog.findViewById(R.id.living_cb6)).setChecked(SocialUI.this.living[6]);
					((CheckBox)humorDialog.findViewById(R.id.living_cb7)).setChecked(SocialUI.this.living[7]);
				}
				
				
				todo.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						if(SocialUI.this.living==null)SocialUI.this.living = new boolean[8];
						SocialUI.this.living[0]=((CheckBox)humorDialog.findViewById(R.id.living_cb0)).isChecked();
						SocialUI.this.living[1]=((CheckBox)humorDialog.findViewById(R.id.living_cb1)).isChecked();
						SocialUI.this.living[2]=((CheckBox)humorDialog.findViewById(R.id.living_cb2)).isChecked();
						SocialUI.this.living[3]=((CheckBox)humorDialog.findViewById(R.id.living_cb3)).isChecked();
						SocialUI.this.living[4]=((CheckBox)humorDialog.findViewById(R.id.living_cb4)).isChecked();
						SocialUI.this.living[5]=((CheckBox)humorDialog.findViewById(R.id.living_cb5)).isChecked();
						SocialUI.this.living[6]=((CheckBox)humorDialog.findViewById(R.id.living_cb6)).isChecked();
						SocialUI.this.living[7]=((CheckBox)humorDialog.findViewById(R.id.living_cb7)).isChecked();
						humorDialog.dismiss();
					}
				});
				humorDialog.show();
			}
		});
		
	}

	public void setUpWidgets(View v) {
		changed = true;
		myView = v;
		SetUpSpinners(v);
		SetUpButtons();
	}

	public String toString() {
		return "Social";
	}

	@Override
	public void importPro(Profile p) {
		Social s = (Social) p;

		((Spinner) myView.findViewById(R.id.children_chooser)).setSelection(s.children);
		((Spinner) myView.findViewById(R.id.ethinic_chooser)).setSelection(s.ethnicity);
		((Spinner) myView.findViewById(R.id.religion_chooser)).setSelection(s.religion);
		((Spinner) myView.findViewById(R.id.political_chooser)).setSelection(s.polView);
		((Spinner) myView.findViewById(R.id.sexual_chooser)).setSelection(s.sexOri);
		((Spinner)myView.findViewById(R.id.smoking_chooser)).setSelection(s.smoking);
		((Spinner)myView.findViewById(R.id.drinking_chooser)).setSelection(s.drinking);
		((Spinner)myView.findViewById(R.id.pets_chooser)).setSelection(s.pets);
		((EditText)myView.findViewById(R.id.hometown_text)).setText(s.hometown);
		((EditText)myView.findViewById(R.id.webpage_text)).setText(s.webpage);
		((EditText)myView.findViewById(R.id.aboutme_text)).setText(s.aboutMe);
		((EditText)myView.findViewById(R.id.passions_text)).setText(s.passions);
		((EditText)myView.findViewById(R.id.sports_text)).setText(s.sports);
		((EditText)myView.findViewById(R.id.activities_text)).setText(s.activities);
		((EditText)myView.findViewById(R.id.books_text)).setText(s.books);
		((EditText)myView.findViewById(R.id.music_text)).setText(s.music);
		((EditText)myView.findViewById(R.id.tv_text)).setText(s.tvShows);
		((EditText)myView.findViewById(R.id.movies_text)).setText(s.movies);
		((EditText)myView.findViewById(R.id.cussines_text)).setText(s.cussines);
	
	}
	
	
	@Override
	public Profile export() {
		if (changed) {
			Social social = new Social();

			social.children = ((Spinner) myView
					.findViewById(R.id.children_chooser))
					.getSelectedItemPosition();

			social.ethnicity = ((Spinner) myView
					.findViewById(R.id.ethinic_chooser)).getSelectedItemPosition();

			social.religion = ((Spinner) myView
					.findViewById(R.id.religion_chooser))
					.getSelectedItemPosition();

			social.polView = ((Spinner) myView
					.findViewById(R.id.political_chooser))
					.getSelectedItemPosition();

			social.sexOri = ((Spinner) myView.findViewById(R.id.sexual_chooser))
					.getSelectedItemPosition();

			social.smoking = ((Spinner) myView
					.findViewById(R.id.smoking_chooser)).getSelectedItemPosition();

			social.drinking = ((Spinner) myView
					.findViewById(R.id.drinking_chooser))
					.getSelectedItemPosition();

			social.pets = ((Spinner) myView.findViewById(R.id.pets_chooser))
					.getSelectedItemPosition();

			social.hometown = ((EditText) myView
					.findViewById(R.id.hometown_text)).getText().toString();

			social.webpage = ((EditText) myView.findViewById(R.id.webpage_text))
					.getText().toString();

			social.aboutMe = ((EditText) myView.findViewById(R.id.aboutme_text))
					.getText().toString();

			social.passions = ((EditText) myView
					.findViewById(R.id.passions_text)).getText().toString();

			social.sports = ((EditText) myView.findViewById(R.id.sports_text))
					.getText().toString();

			social.activities = ((EditText) myView
					.findViewById(R.id.activities_text)).getText().toString();

			social.books = ((EditText) myView.findViewById(R.id.books_text))
					.getText().toString();

			social.music = ((EditText) myView.findViewById(R.id.music_text))
					.getText().toString();

			social.tvShows = ((EditText) myView.findViewById(R.id.tv_text))
					.getText().toString();

			social.movies = ((EditText) myView.findViewById(R.id.movies_text))
					.getText().toString();

			social.cussines = ((EditText) myView
					.findViewById(R.id.cussines_text)).getText().toString();

			return social;
		}
		return null;
	}

}
